package vn.sunnet.backviet01.game.coloa.human.enemy;

import java.util.Arrays;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import vn.sunnet.backviet01.game.coloa.constant.Constants;

public class Spell extends Enemy {
	private long durationAttack = 200;
	private long durationMove = 100;
	private long durationDie = 100;
	
	private long countDelay = 0;
	
	public Spell(int tiledAttack, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(tiledAttack, pX, pY, pTiledTextureRegion);
		this.name = Constants.SPELL;
	}
	
	public void onManagedUpdate(final float pSecondsElapsed) {
		if (isAllowChangeStatus) {
			if (status == Constants.DIE) {
				if (getTextureRegion() != mTextureRegionMove)
					updateTextureRegion(mTextureRegionMove, true);
				die();
			} else if (isFreezed && countDelay == 0) {
				 	this.stopAnimation();
					updateTextureRegion(mTextureRegion_next);
					countDelay = System.currentTimeMillis();
					this.handler.setEnabled(false);
			} else if (isStun && startStun == 0) {
				startStun = System.currentTimeMillis();
				setStunAction();
			} else if (status == Constants.ATTACK) {
					this.attack();
			}
			isAllowChangeStatus = false;
		}   else if (isFreezed && this.hitPoints > 0) {
			if (countDelay + timeFreezed < System.currentTimeMillis()) {
				if (status == Constants.MOVE) {
					move();
					handler.setEnabled(true);
				} else if (status == Constants.ATTACK) {
					attack();
				}
				isFreezed = false;
				countDelay = 0;
			}
		} else if (isStun && this.hitPoints > 0) {
			if (timeStun + startStun < System.currentTimeMillis()) {
				resetStunAction();
				isStun = false;
				startStun = 0;
			}
		}
		
		if (status == Constants.ATTACK
				&& this.getCurrentTileIndex() != tiledAttack)
			isAttack = false;

		else if (status == Constants.DIE && this.life) {
			setAlpha(getAlpha() - 0.02f);
		}
		
		xContain = this.getX();
		yContain = this.getY();
		
		super.onManagedUpdate(pSecondsElapsed);
	}

	protected void createContain() {
		xContain = this.getX();
		yContain = this.getY();

		widthContain = this.getWidth();
		heightContain = this.getHeight();
	}

	public void setDurationAttack(long durationAttack) {
		this.durationAttack = durationAttack;
	}

	public long getDurationMove() {
		return durationMove;
	}

	public void setDurationMove(long durationMove) {
		this.durationMove = durationMove;
	}

	public long getDurationDie() {
		return durationDie;
	}

	public void setDurationDie(long durationDie) {
		this.durationDie = durationDie;
	}

	public void move() {
		long[] duration = new long[6];
		Arrays.fill(duration, durationMove);
		this.stopAnimation();
		if (this.getTextureRegion() != mTextureRegionMove)
			updateTextureRegion(mTextureRegionMove, true);
		this.animate(duration, 8, 13, true);
		this.setCurrentTileIndex(8);
	}
	
	public void attack() {
		long[] duration = new long[8];
		Arrays.fill(duration, durationAttack);
		this.stopAnimation();
		if (this.getTextureRegion() != mTextureRegionMove) 
			updateTextureRegion(mTextureRegionMove, true);
		this.unregisterUpdateHandler(handler);
		this.animate(duration, 0, 7, true);
		this.setCurrentTileIndex(0);
	}
	
	public void die() {
		long[] duration = new long[6];
		Arrays.fill(duration, durationDie);
		this.stopAnimation();
		this.animate(duration, 14, 19, true);
		this.setCurrentTileIndex(14);
	}
	

	public void setStunAction() {
		
		switch (status) {
		case Constants.MOVE:
			long[] duration1 = new long[6];
			Arrays.fill(duration1, durationMove * 2);
			int index = this.getCurrentTileIndex();
			animate(duration1, 8, 13, true);
			handler.setVelocityX(handler.getVelocityX() / 2);
			setCurrentTileIndex(index);
			
			break;
		case Constants.ATTACK:
			long[] duration = new long[8];
			Arrays.fill(duration, durationAttack * 2);
			int index1 = this.getCurrentTileIndex();
			animate(duration, 0, 7, true);
			setCurrentTileIndex(index1);
			break;
		default:
			break;
		}
	}
	
	public void resetStunAction() {
		switch (status) {
		case Constants.MOVE:
			long[] duration1 = new long[6];
			Arrays.fill(duration1, durationMove * 2);
			int index = this.getCurrentTileIndex();
			animate(duration1, 8, 13, true);
			handler.setVelocityX(handler.getVelocityX() * 2);
			setCurrentTileIndex(index);
			
			break;
		case Constants.ATTACK:
			long[] duration = new long[8];
			Arrays.fill(duration, durationAttack);
			int index1 = this.getCurrentTileIndex();
			animate(duration, 0, 7, true);
			setCurrentTileIndex(index1);
			break;
		default:
			break;
		}
	}
	
	
}
